#登录页面
from selenium.webdriver.common.by import By
from base.base import Base
from util.img_util import imgcode_file
import time

class PageLogin(Base):
    username_input_locator = (By.NAME,'username')
    passwd_input_locator = (By.NAME,'password')
    code_input_locator = (By.NAME,'captchaAnswer')
    code_locator = (By.XPATH,'//span/img')
    login_bt_locator = (By.XPATH,'//span[text()="登录"]')
    err_message_locator = (By.CLASS_NAME,'el-form-item__error')
    popup_err_message_locator = (By.CLASS_NAME,'el-message__content')
    title_locator = (By.XPATH,'//div[@class="ele-admin-logo"]/span ')
    #输入用户名
    def input_username(self,username):
        self.input(self.username_input_locator,username)
    #输入密码
    def input_passwd(self,passwd):
        self.input(self.passwd_input_locator,passwd)
    #输入验证码
    def input_code(self):
        file_location = 'D:\\img/code.png'
        elm = self.find(self.code_locator)
        time.sleep(1)
        elm.screenshot(file_location)
        code = imgcode_file(file_location)
        self.input(self.code_input_locator,code)

    def clear_inputs(self):
        self.clear(self.username_input_locator)
        self.clear(self.passwd_input_locator)
        self.clear(self.code_input_locator)
    #点击登录按钮
    def click_login_bt(self):
        self.click(self.login_bt_locator)
    #获取报错信息
    def get_err_message(self):
        errs = []
        errs.append(self.finds(self.err_message_locator)[0].text)
        errs.append(self.finds(self.err_message_locator)[1].text)
        return errs       
    def get_popup_err_message(self):
        return self.find(self.popup_err_message_locator).text
    def get_title_message(self):
        return self.find(self.title_locator).text
